#include <modul.h>

extern int zgbfd;

volatile unsigned char *p = 0;

int zigbee_get_dat(int fd, env_dev_rfid_card *env_dev)//M0通过ZigBee发来的信息解析
{
	
	int ret;
	unsigned char buf[ZGB_DATA_SIZE];
	//printf("##########################################11111\n");
	memset(buf, 0, sizeof(buf));
	ret = serial_recv_exact_nbytes(fd, buf, sizeof(buf));
	if (0 > ret)
	{
		return ERR;
	}
	//printf("##########################################33333\n");
	int i;
	printf("\n\n");
	for (i = 0; i < 36; i++){
		printf("%.2x ", buf[i]);
	}
	printf("\n");
	
	if (buf[0] == 0xBB) {//M0通过ZigBee发来的环境信息解析
		memset(env_dev, 0, sizeof(env_dev_rfid_card));
		env_dev->env_data.tem =  (int)buf[5] + buf[4] ;
		env_dev->env_data.hum =  (int)buf[7] + buf[6] ;
		env_dev->env_data.lux =  (int)((buf[23] << 24) + (buf[22] << 16) + (buf[21] << 8) + buf[20]);
		printf("env_dev->env_data.tem = %f\tenv_dev.env_data.hum = %f\tenv_dev.env_data.lux = %f\n",env_dev->env_data.tem,env_dev->env_data.hum,env_dev->env_data.lux);

		env_dev->dev.sunshade = (int)buf[24];
		env_dev->dev.air_fan = (int)buf[25];
		env_dev->dev.spray_water = (int)buf[26];
		printf("env_dev.dev.air_fan = %d\tenv_dev.dev.sunshade = %d\tenv_dev.dev.spray_water = %d\n",env_dev->dev.air_fan,env_dev->dev.sunshade,env_dev->dev.spray_water);
		//p = "FAN_ON";
		//zigbee_exe_cmd(fd,p);
	}
	if (buf[0] == 0xCC)
	{
		env_dev->rfid_card = (int)((buf[7]<<24) + (buf[6]<<16) + (buf[5]<<8) + buf[4]);
		printf("rfid_card = %d\n",env_dev->rfid_card);
	}
	if (buf[0] == 0xee)
	{
		env_dev->rfid_card = -1;
	}
	printf("\n\n");
	//printf("##########################################22222\n");
	return OK;	
}

int zigbee_exe_cmd(int fd,  unsigned char *p)
{
	unsigned char buf[ZGB_DATA_SIZE] = {0xdd, 0x01, 0x24, 0x00, 0x00};
	if (strstr(p, "LIGHT_ON") != NULL){//LED1 ON
		buf[4] = 0x00;
	} else if (strstr(p, "LIGHT_OFF") != NULL){//LED1
		buf[4] = 0x01;
	} else if (strstr(p, "BUZZ_ON") != NULL){
		buf[4] = 0x02;
	} else if (strstr(p, "BUZZ_OFF") != NULL){
		buf[4] = 0x03;
	} else if (strstr(p, "FAN_ON") != NULL){
		buf[4] = 0x04;
	} else if (strstr(p, "FAN_OFF") != NULL){
		buf[4] = 0x08;
	} else if (strstr(p, "SHU_ON") != NULL){
		buf[4] = 0x09;
	} else if (strstr(p, "SHU_OFF") != NULL){
		buf[4] = 0x0a;
	} else if (strstr(p, "DOR_ON") != NULL){
		buf[4] = 0x05;
	} else if (strstr(p, "DOR_OFF") != NULL){
		buf[4] = 0x06;
	}
	
	int ret = serial_send_exact_nbytes(fd, buf, sizeof(buf));
	return ret;
}


